<?php
/**
 * Created by PhpStorm.
 * User: denghp
 * Date: 15-6-8
 * Time: 下午2:58
 */

namespace backend\controllers;

use Yii;
use yii\rbac\Item;
use yii\web\Controller;
use linslin\yii2\curl;
use yii\helpers\Url;
use yii\web\ForbiddenHttpException;
/**
 * Site controller
 */
class BaseController extends Controller {

    protected $menuList;

    public $module;

    public function init() {

    }

    public function beforeAction($action)
    {
        $startTime = time();

//        if (\Yii::$app->user->can('admin')) {
//            return true;
//        }
//        $action = Yii::$app->controller->action->id;
//        //list默认继承index操作权限,否则index跳转页面无法通过listAction获取数据
//        if ($action == 'list' && \Yii::$app->user->can('index')) {
//            return true;
//        }
//        $roles = Yii::$app->getAuthManager()->getRolesByUser(\Yii::$app->user->id);
//        $activeRoute = preg_replace('[(/index)|(/create)|(/delete)|(/update)|(/view)]','',\Yii::$app->controller->route);
//        $module = Yii::$app->controller->module->id;
//        //判断用户所在的角色,是否对相应的route授权
//        $valid_route = false;
//        foreach ($roles as $role) {
//            $childrens = Yii::$app->getAuthManager()->getChildren($role->name);
//            foreach ($childrens as $child) {
//                if ($child->type == Item::TYPE_ROLE || $child->name[0] !== '/'){
//                    continue;
//                }
//                if (empty($child) or ('/'.$module.'/*' == $child->name) or strpos($child->name,$activeRoute) !== false) {
//                    $valid_route = true;
//                    break;
//                }
//            }
//        }
//        if($valid_route && \Yii::$app->user->can($action)){
//            Yii::trace("beforeAction useTime : ".time() - $startTime);
//            return true;
//        } else{
//            Yii::trace("beforeAction useTime : ".time() - $startTime);
//            throw new ForbiddenHttpException('对不起，您现在还没获此操作的权限');
//        }
    }

}
